//
//  SchemeTable.m
//  cutter
//
//  Created by mac on 16/1/6.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "MatchEdit.h"
#import "SchemeCustom.h"
#import "SchemeSelector.h"
#import "SchemeCenterCtrl.h"
#import "SpaceEditController.h"

//
@implementation SchemeMutableButton
-(void)setSelected:(BOOL)selected{
    if (self.selected != selected) {
        [super setSelected:selected];
        //
        UIImage *a = [self imageForState:UIControlStateHighlighted];
        UIImage *b = [self imageForState:UIControlStateDisabled];
        [self setImage:b forState:UIControlStateHighlighted];
        [self setImage:a forState:UIControlStateDisabled];
        [self setNeedsLayout];
    }
}
@end

//
@implementation SchemeSelectorTabbed
- (void)perform{
    UIViewController *source = self.sourceViewController;
    UIViewController *destination = self.destinationViewController;
    for (UIViewController *c in source.childViewControllers) {
        [c.view removeFromSuperview];
        [c removeFromParentViewController];
    }
    [source.view insertSubview:destination.view atIndex:0];
    [source addChildViewController:destination];
}
@end


//
@implementation SchemeCustomTable
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //
    if (self.presentationController) {
        UIView *parent = [[self.navigationController view] superview];
        if (parent) {
            [parent.layer setCornerRadius:6];
        }else{
            parent = [self.view superview];
            if (parent) {
                [parent.layer setCornerRadius:6];
            }
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.1;
    }
    return tableView.sectionHeaderHeight;
}
@end


//
@implementation SchemeCustomPopover
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //
    if (self.presentationController) {
        UIView *parent = [self.view superview];;
        if (parent) {
            [parent.layer setCornerRadius:6];
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.onSelected && indexPath.row == 0) {
        if(self.type==1){
            UIStoryboard*story=[UIStoryboard storyboardWithName:@"Space" bundle:nil];
            SpaceEditController *eidt =[story instantiateViewControllerWithIdentifier:@"SpaceEdit"];
            [eidt setTitleName:self.str];
            [eidt setSource:self.dat];
            [self presentViewController:eidt animated:YES completion:nil];
        }
        if(self.type==2)
        {
            UIStoryboard*story=[UIStoryboard storyboardWithName:@"MatchCenter" bundle:nil];
            MatchEdit *eidt =[story instantiateViewControllerWithIdentifier:@"MatchEdit"];
            [eidt import:self.dat];
            [eidt setOnSelected:^(id target, id result){
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [self presentViewController:eidt animated:YES completion:nil];
        }
    }
    if (self.onSelected && indexPath.row > 4) {
        self.onSelected(self, indexPath);
    }
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.destinationViewController isKindOfClass:UINavigationController.class]) {
        UINavigationController *destination = segue.destinationViewController;
        if ([[destination.viewControllers firstObject] isKindOfClass:SchemeCenterAdd.class]) {
            SchemeCenterAdd *ctrl = [destination.viewControllers firstObject];
            [ctrl setOnSelected:self.onSelected];
            [ctrl setAppend:YES];
        }
    }
    
//    if ([segue.destinationViewController isKindOfClass:SpaceEditController.class]) {
//        SpaceEditController *ctrl = segue.destinationViewController;
//        ctrl.classids = self.classids;
//        [ctrl setTitleName:self.str];
//        [ctrl setSource:self.dat];
//        [ctrl setOnSelected:self.onSelected];
//    }
    if ([segue.destinationViewController isKindOfClass:SchemeSelector.class]) {
        SchemeSelector *ctrl = segue.destinationViewController;
        ctrl.classids = self.classids;
        [ctrl setOnSelected:self.onSelected];
    }
    if ([segue.destinationViewController isKindOfClass:MatchEdit.class]) {
        MatchEdit *ctrl = segue.destinationViewController;
        //ctrl.classids = self.classids;
        [ctrl setOnSelected:self.onSelected];
    }
    if ([segue.destinationViewController isKindOfClass:ProductCenterCtrl.class]) {
        ProductCenterCtrl *ctrl = segue.destinationViewController;
        [ctrl setOnSelected:self.onSelected];
        [ctrl setType:1];
    }
}
@end



@implementation SchemeSinglePopover
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //
    if (self.presentationController) {
        UIView *parent = [self.view superview];;
        if (parent) {
            [parent.layer setCornerRadius:6];
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.onSelected&& indexPath.row == 1) {
        self.onSelected(self, indexPath);
    }
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.destinationViewController isKindOfClass:ProductCenterCtrl.class]) {
        ProductCenterCtrl *ctrl = segue.destinationViewController;
        [ctrl setOnSelected:self.onSelected];
        [ctrl setType:1];
    }
}
@end